package com.fakeDYChat.common.websocket.service.adapter;

import com.fakeDYChat.common.common.domain.enums.YesOrNoEnum;
import com.fakeDYChat.common.user.domain.entity.User;
import com.fakeDYChat.common.websocket.domain.vo.resp.WSBaseResp;
import com.fakeDYChat.common.websocket.domain.enums.WSRespTypeEnum;
import com.fakeDYChat.common.websocket.domain.vo.resp.WSBlack;
import com.fakeDYChat.common.websocket.domain.vo.resp.WSLoginSuccess;
import com.fakeDYChat.common.websocket.domain.vo.resp.WSLoginUrl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

/**
 * 消息封装器
 *
 * @date 2023/12/10 14:34
 **/
public class WSAdapter {
    /**
     * 登录链接
     *
     * @param codeTicket
     * @return com.fakeDYChat.common.user.domain.enums.WSBaseResp<com.fakeDYChat.common.user.domain.vo.resp.WSLoginUrl>
     * @author Tang
     * @date 2023/12/10 14:39
     **/
    public static WSBaseResp<WSLoginUrl> buildLoginResp(WxMpQrCodeTicket codeTicket) {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        WSLoginUrl wsLoginUrl = WSLoginUrl.builder().loginUrl(codeTicket.getUrl()).build();
        wsBaseResp.setData(wsLoginUrl);
        return wsBaseResp;
    }

    /**
     * 扫码成功
     *
     * @return com.fakeDYChat.common.websocket.domain.vo.resp.WSBaseResp<?>
     * @author Tang
     * @date 2023/12/11 10:35
     **/
    public static WSBaseResp<?> buildScanSuccessResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    /**
     * 封装登录成功信息
     *
     * @param user
     * @param token
     * @param b
     * @return void
     * @author Tang
     * @date 2023/12/11 11:25
     **/
    public static WSBaseResp<WSLoginSuccess> buildLoginSuccess(User user, String token, boolean power) {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .avatar(user.getAvatar())
                .name(user.getName())
                .power(power ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus())
                .uid(user.getId())
                .token(token)
                .build();
        wsBaseResp.setData(wsLoginSuccess);
        return wsBaseResp;
    }

    /**
     * token失效
     *
     * @param
     * @return com.fakeDYChat.common.websocket.domain.vo.resp.WSBaseResp<?>
     * @author Tang
     * @date 2023/12/22 10:01
     **/
    public static WSBaseResp<?> buildInvalidTokenResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }

    /**
     * 拉黑消息
     *
     * @param user
     * @return com.fakeDYChat.common.websocket.domain.vo.resp.WSBaseResp<?>
     * @author Tang
     * @date 2024/1/12 20:58
     **/
    public static WSBaseResp<?> buildBlack(User user) {
        WSBaseResp<WSBlack> resp = new WSBaseResp<>();
        WSBlack black = new WSBlack(user.getId());
        resp.setData(black);
        resp.setType(WSRespTypeEnum.BLACK.getType());
        return resp;
    }
}
